package com.zeng.sim.controller;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.sim.controller.domain.SimMsgRecord;
import com.zeng.sim.controller.domain.msg.MsgData;
import com.zeng.sim.controller.domain.request.PageDomain;
import com.zeng.sim.controller.domain.response.Result;
import com.zeng.sim.controller.domain.response.ResultCode;
import com.zeng.sim.controller.domain.response.ResultTable;
import com.zeng.sim.mapper.model.SimMsgRecordEntity;
import com.zeng.sim.service.ClientMsgService;
import com.zeng.sim.service.SimMsgRecordService;
import com.zeng.sim.util.SimConstant;
import com.zeng.sim.util.SysConfigUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述信息
 *
 * @author zengwanlin
 * @date 2023-12-21T13:52
 */
@Controller
@RequestMapping("/client/msg")
public class ClientMsgController {

    @Autowired
    private ClientMsgService clientMsgService;

    @Autowired
    private SimMsgRecordService simMsgRecordService;

    @GetMapping("/sendSocket")
    public String sendSocketMain(Model model) {
        model.addAttribute("hostname",
                SysConfigUtil.getSystemConfigValue(SysConfigUtil.CONFIG_CODE_CLIENT_SIM_SERVER_HOSTNAME));
        model.addAttribute("port",
                SysConfigUtil.getSystemConfigValue(SysConfigUtil.CONFIG_CODE_CLIENT_SIM_SERVER_PORT));
        return "client/msg/send/socket";
    }

    @GetMapping("/sendHttp")
    public String sendHttpMain(Model model) {
        model.addAttribute("url", SysConfigUtil.getSystemConfigValue(SysConfigUtil.CONFIG_CODE_CLIENT_SIM_SERVER_URL));
        return "client/msg/send/http";
    }

    @GetMapping("/list")
    public String list() {
        return "client/msg/list/main";
    }

    @GetMapping("/add")
    public String add(Model model) {
        model.addAttribute("busiType", SimConstant.BUSI_TYPE_01);
        return "client/msg/list/add";
    }

    @GetMapping("/edit")
    public String edit(Model model, String id) {
        SimMsgRecordEntity simMsgRecordEntity = simMsgRecordService.getById(id);
        SimMsgRecord simMsgRecord = new SimMsgRecord();
        BeanUtils.copyProperties(simMsgRecordEntity, simMsgRecord);
        model.addAttribute("simMsgRecord", simMsgRecord);
        return "client/msg/list/edit";
    }

    @PostMapping("/sendSocket")
    @ResponseBody
    public Result<String> sendSocketMsg(@RequestBody MsgData msgData) {
        String respMsg = clientMsgService.sendMsg(msgData, SimConstant.MsgProtocol.MSG_PROTOCOL_SOCKET);
        return Result.success(ResultCode.SUCCESS.getMessage(), respMsg);
    }

    @PostMapping("/sendHttp")
    @ResponseBody
    public Result<String> sendHttpMsg(@RequestBody MsgData msgData) {
        String respMsg = clientMsgService.sendMsg(msgData, SimConstant.MsgProtocol.MSG_PROTOCOL_HTTP);
        return Result.success(ResultCode.SUCCESS.getMessage(), respMsg);
    }

    @GetMapping("/data")
    @ResponseBody
    public ResultTable data(SimMsgRecord msgRecord, PageDomain pageDomain) {
        Page<SimMsgRecord> page = clientMsgService.data(msgRecord, pageDomain);
        return ResultTable.pageTable(page);
    }

    @PostMapping("/batchSend/{ids}")
    @ResponseBody
    public Result batchSend(@PathVariable String ids) {
        List<Integer> idList = ListUtil.toList(ids.split(","))
            .stream()
            .mapToInt(Integer::parseInt)
            .boxed()
            .collect(Collectors.toList());
        clientMsgService.batchSend(idList);
        return Result.success();
    }

}
